//
//  htvmyorder.m
//  WZYApp
//
//  Created by zhangwq on 16/1/28.
//  Copyright © 2016年 HT. All rights reserved.
//

#import "htvmyorder.h"
#import "HTSysParam.h"
#import "HTFrameNet.h"
#import "UIImageView+WebCache.h"

@interface htvmyorder (){
    CGFloat width;
    CGFloat height;
    UIColor *backColor;
    UIView *uv;
    NSMutableArray *orderArray;
    UIWebView *webview;
}

@property (nonatomic, strong) NSOperationQueue *queue;

@end

@implementation htvmyorder

- (NSOperationQueue *)queue {
    if (!_queue) _queue = [[NSOperationQueue alloc] init];
    return _queue;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGRect rect = [[UIScreen mainScreen] bounds];
    CGSize size = rect.size;
    width = size.width;
    height = size.height;
    
    [self loading];
    
    orderArray=[NSMutableArray array];
    uv=[[UIView alloc]init];
    backColor=[UIColor colorWithRed:150.0/255 green:150.0/255 blue:150.0/255 alpha:1.0f];
    
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    
    self.tableView.separatorStyle=NO;
    
    [self BindList];
    
}


-(void)loading{
    
    self.tableView.scrollEnabled=NO;
    
    webview=[[UIWebView alloc]initWithFrame:CGRectMake(-10, 0, width+10, height)];
    webview.backgroundColor=[UIColor colorWithRed:255.0/255 green:255.0/255 blue:255.0/255 alpha:1.0f];
    webview.opaque=NO;
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *resPath = [bundle resourcePath];
    NSString *filePath = [resPath stringByAppendingPathComponent:@"loading.html"];
    
    [webview loadHTMLString:[NSString stringWithContentsOfFile:filePath]
                    baseURL:[NSURL fileURLWithPath:[bundle bundlePath]]];
    [self.tableView addSubview:webview];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


//刷新数据
-(void)BindList
{
    NSString *serverUrl=[[NSString alloc]initWithFormat:@"%@\/%@\/",[HTSysParam erpServer],[HTSysParam erpVirDir]];
    HTFrameNet *htnet=[[HTFrameNet alloc] initWithServerUrl:serverUrl];
    
    NSDictionary *dict = @{@"memberid":[HTSysParam userID],@"stateid":@9,@"pagernationVo": @{@"currPage": @1,@"pageSize": @100}};
    [htnet hmNetWCFmethodName:@"web/memberOrder/querySaleMemberOrderByOrder" andDic:dict complete:^(NSString *backstr)
     {
         NSDictionary *rootDict=[HTFrameNet hmConvertDictFromJsonStr:backstr];
         NSDictionary *body=[rootDict objectForKey:@"body"];
         if(body!=nil){
             
             NSDictionary *SaleMemberOrder=[body objectForKey:@"SaleMemberOrder"];
             orderArray=[SaleMemberOrder objectForKey:@"data"];
             [self.tableView reloadData];
             
             [webview removeFromSuperview];
             self.tableView.scrollEnabled=YES;
             
             
         }
         
         
         
         
     }];
    
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [orderArray count];
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 130.0;
}


- ( UITableViewCell *)tableView:( UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath{
    
    NSString *cellId = @"cellname" ;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier :cellId];
    if (cell == nil ) {
        cell = [[ UITableViewCell alloc ] initWithStyle : UITableViewCellStyleDefault reuseIdentifier :cellId] ;
    }
    
    NSDictionary *dict=[orderArray objectAtIndex:[indexPath row]];
    
    uv=[[UIView alloc]initWithFrame:CGRectMake(0, 0, width, 130)];
    
    UILabel *lbordernum=[[UILabel alloc]initWithFrame:CGRectMake(25, 5, width-100, 20)];
    lbordernum.text=[[NSString alloc]initWithFormat:@"订单号：%@",[dict objectForKey:@"orderNum"] ];
    lbordernum.textColor=backColor;
    lbordernum.font=[UIFont fontWithName:@"Arial" size:11.0];
    [uv addSubview:lbordernum];
    
    
    NSString *timeStamp =[dict objectForKey:@"orderDate"];
    long  date = [timeStamp longLongValue];
    NSString *timeString=[self getDateFormate:date];
    
    
    UILabel *lborderdate=[[UILabel alloc]initWithFrame:CGRectMake(190, 5, width-100, 20)];
    lborderdate.text=[[NSString alloc]initWithFormat:@"日期：%@",timeString ];
    lborderdate.textColor=backColor;
    lborderdate.font=[UIFont fontWithName:@"Arial" size:11.0];
    [uv addSubview:lborderdate];
    
    
    
    UILabel *lbitemmodel=[[UILabel alloc]initWithFrame:CGRectMake(100, 30, width-100, 20)];
    lbitemmodel.text=[[NSString alloc]initWithFormat:@"%@ %@",[dict objectForKey:@"itemstd"],[dict objectForKey:@"itemname"] ];
    lbitemmodel.textColor=backColor;
    lbitemmodel.font=[UIFont fontWithName:@"Arial" size:11.0];
    [uv addSubview:lbitemmodel];
    
    UILabel *lbcolornum=[[UILabel alloc]initWithFrame:CGRectMake(100, 50, 85, 20)];
    lbcolornum.text=[[NSString alloc]initWithFormat:@"色号：%@",[self checkValue:[dict objectForKey:@"colorNum"]] ];
    lbcolornum.textColor=backColor;
    lbcolornum.font=[UIFont fontWithName:@"Arial" size:11.0];
    [uv addSubview:lbcolornum];
    
    
    UILabel *lbcolorname=[[UILabel alloc]initWithFrame:CGRectMake(190, 50, 100, 20)];
    lbcolorname.text=[[NSString alloc]initWithFormat:@"颜色：%@",[self checkValue:[dict objectForKey:@"colorName"]] ];
    lbcolorname.textColor=backColor;
    lbcolorname.font=[UIFont fontWithName:@"Arial" size:11.0];
    [uv addSubview:lbcolorname];
    
    UILabel *lbbatch=[[UILabel alloc]initWithFrame:CGRectMake(100, 70, 85, 20)];
    lbbatch.text=[[NSString alloc]initWithFormat:@"批号：%@",[self checkValue:[dict objectForKey:@"batch"]] ];
    lbbatch.textColor=backColor;
    lbbatch.font=[UIFont fontWithName:@"Arial" size:11.0];
    [uv addSubview:lbbatch];
    
    
    UILabel *lbqty=[[UILabel alloc]initWithFrame:CGRectMake(190, 70, 100, 20)];
    lbqty.text=[[NSString alloc]initWithFormat:@"数量：%@",[dict objectForKey:@"qty"] ];
    lbqty.textColor=backColor;
    lbqty.font=[UIFont fontWithName:@"Arial" size:11.0];
    [uv addSubview:lbqty];
    
    
    UILabel *lbprice=[[UILabel alloc]initWithFrame:CGRectMake(100, 90, 85, 20)];
    lbprice.text=[[NSString alloc]initWithFormat:@"单价：%@",[dict objectForKey:@"singlePrice"] ];
    lbprice.textColor=backColor;
    lbprice.font=[UIFont fontWithName:@"Arial" size:11.0];
    [uv addSubview:lbprice];
    
    
    UILabel *lbamount=[[UILabel alloc]initWithFrame:CGRectMake(190, 90, 100, 20)];
    lbamount.text=[[NSString alloc]initWithFormat:@"金额：%@",[dict objectForKey:@"amount"] ];
    lbamount.textColor=backColor;
    lbamount.font=[UIFont fontWithName:@"Arial" size:11.0];
    [uv addSubview:lbamount];
    
    
    NSMutableArray *dataPicList=[dict objectForKey:@"dataPicList"];
    if([dataPicList count]>0){
        NSDictionary *pic=[dataPicList objectAtIndex:0];
        UIImageView *iv=[[UIImageView alloc]initWithFrame:CGRectMake(25,35, 70,70)];
        iv.accessibilityLabel=[pic objectForKey:@"picPath"];
        [iv setClipsToBounds:YES];
        [iv setContentMode:UIViewContentModeScaleAspectFill];
        iv.layer.masksToBounds=YES;
        iv.layer.cornerRadius = 2.0;
        iv.userInteractionEnabled=YES;
        [self downloadImg:iv];
        [uv addSubview:iv];
        
        
    }
    else{
        UIImageView *iv=[[UIImageView alloc]initWithFrame:CGRectMake(25,35, 70,70)];
        iv.image=[UIImage imageNamed:@"nopic"];
        [iv setClipsToBounds:YES];
        [iv setContentMode:UIViewContentModeScaleAspectFill];
        iv.layer.masksToBounds=YES;
        iv.layer.cornerRadius = 2.0;
        iv.userInteractionEnabled = YES;
        [uv addSubview:iv];
        

    }
    
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(itemDetailAction:)];
    singleTap.accessibilityLabel=[dict objectForKey:@"itemid"];
    [cell addGestureRecognizer:singleTap];
    
    
    int stateid=[[dict objectForKey:@"stateid"]intValue];
    UIImageView *statusView=[[UIImageView alloc]initWithFrame:CGRectMake(width-55, 40, 60, 60)];
    switch (stateid) {
        case 0:
            statusView.image=[UIImage imageNamed:@"o1"];
            break;
        case 1:
            statusView.image=[UIImage imageNamed:@"o2"];
            break;
        case 2:
            statusView.image=[UIImage imageNamed:@"o3"];
            break;
        case 3:
            statusView.image=[UIImage imageNamed:@"o4"];
            break;
        case 4:
            statusView.image=[UIImage imageNamed:@"o5"];
            break;
            
        default:
            break;
    }
    [uv addSubview:statusView];
    
    
    UIView *line=[[UIView alloc]initWithFrame:CGRectMake(20, 119, width, 0.6)];
    line.backgroundColor=[UIColor colorWithRed:150.0/255.0 green:150.0/255.0 blue:150.0/255.0 alpha:1.0];
    [uv addSubview:line];
    
    
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.accessoryView=uv;
    
    
    
    return cell;
}


-(NSString *)checkValue:(id)value{
    if(value==nil||value==[NSNull null]){
        return @"-";
    }
    else{
        return value;
    }
}


-(NSString *)getDateFormate:(long)dateTimeLong{
    NSDate *dateTime = [[NSDate alloc] initWithTimeIntervalSince1970:dateTimeLong/1000];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setTimeStyle:NSDateFormatterNoStyle];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    NSLocale *formatterLocal = [[NSLocale alloc] initWithLocaleIdentifier:@"en_us"];
    [formatter setLocale:formatterLocal];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateString = [formatter stringFromDate:dateTime];
    
    return dateString;
}

//查看商品详情
-(void)itemDetailAction:(UITapGestureRecognizer*)sender{
    NSString *storyboard=@"ItemDetail";
    NSString *Identifier=@"ItemDetail";
    UIStoryboard *valueView = [UIStoryboard storyboardWithName:storyboard bundle:nil];
    UIViewController *uv=[valueView instantiateViewControllerWithIdentifier:Identifier];
    UINavigationController *baNavigation = [[UINavigationController alloc] initWithRootViewController:uv];
    [uv setValue:@"1" forKey:@"datatypeid"];//不需要强制转换也可以赋值
    [uv setValue:@"" forKey:@"specialvalue"];//不需要强制转换也可以赋值
    [uv setValue:sender.accessibilityLabel forKey:@"formid"];//不需要强制转换也可以赋值
    [self presentModalViewController:baNavigation animated:YES];
    
    //返回
}



- (IBAction)backAction:(UIButton *)sender {
    [self dismissViewControllerAnimated: YES completion: nil ];
}

- (void)downloadImg:(UIImageView *)theImageView {
    
    NSString *imageName=[[NSString alloc]initWithFormat:@"%@?imageView2/2/w/150",theImageView.accessibilityLabel];
    NSURL *imagePath = [NSURL URLWithString:imageName];
    [self.queue addOperationWithBlock: ^{
        
        //在主线程中更新UI
        [[NSOperationQueue mainQueue] addOperationWithBlock: ^{
            
            [theImageView sd_setImageWithURL:imagePath placeholderImage:[UIImage imageNamed:@"nopic"]];
            
            
            
        }];
    }];
}
@end
